package com.think.grpcclient

import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.telecom.CallScreeningService
import android.util.Log
import android.view.View
import android.widget.ScrollView
import android.widget.TextView
import androidx.annotation.IdRes
import androidx.core.widget.NestedScrollView
import com.think.grpcclient.R

//LocalIp:  192.168.0.195
class ClientActivity : AppCompatActivity() {

    val mHandler by lazy {
        Handler(Looper.getMainLooper())
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val response = findViewById<TextView>(R.id.response)

        val callResponse = object : CallResponse {
            override fun onBlock(data: String) {
                mHandler.post {
                    val lastText = response.text
                    response.text = "${lastText} \n ${data}"
                    mHandler.postDelayed({
                        findViewById<NestedScrollView>(R.id.scrollView).fullScroll(ScrollView.FOCUS_DOWN)
                    }, 100)
                }
            }
        }

        clickView(R.id.clear) {
            response.text = ""
        }

        clickView(R.id.init) {
            try {
                Client.init(callResponse)
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }
        }


        clickView(R.id.sayHello) {
            try {
                Client.sayHello("JJ")
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }
        }

        clickView(R.id.getSerTime){

            try {
                Client.getSerTime()
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }

        }

        clickView(R.id.getSerStreamTime){
            try {
                Client.getSerStreamTime()
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }

        }

        clickView(R.id.uploadFileStream){
            try {
                Client.uploadFileStream()
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }

        }

        clickView(R.id.downloadStream){
            try {
                Client.downloadStream()
            } catch (e: Exception) {
                callResponse.onBlock("${e.message}")
            }

        }

    }
}


object LogUtil {
    fun d(tag: String, info: String?) {
        Log.d("guoyh_Grpc_${tag}", "[-${info}-]")
    }

    fun d(info: String) {
        Log.d("guoyh_Grpc_Client", info)
    }
}


fun Activity.clickView(@IdRes id: Int, it: (View) -> Unit) {
    findViewById<View>(id).click(it)
}

fun View.click(it: (View) -> Unit) {
    setOnClickListener(it)
}

interface CallResponse {
    fun onBlock(data: String)
}